package com.sgb.kjwl.view.widget;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sdk.mobile.manager.login.NoDoubleClickListener;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.lease.LeaseMapEntity;
import com.sgb.kjwl.view.ui.activity.self.userInfo.VerificationCodeLoginActivity;
import com.sgb.member.MemberConfig;
import com.sgb.member.entity.QualityMemberBean;
import com.sgb.member.entity.QualityMemberBean2;
import com.sgb.member.ui.activity.ComApplyOrInvitePersonActivity;
import com.sgb.member.ui.activity.ComApplyOrInvitePersonShenPiActivity;
import com.sgb.member.ui.activity.MemberApplyJoinActivity;
import com.sgb.member.ui.activity.MemberInviteJoinActivity;
import com.sgb.member.ui.activity.PersonApplyJoinActivity;
import com.sgb.member.ui.activity.PersonInviteJoinActivity;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.CommonDialog;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.dialog.RoundDialog;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;

public class MapInfoDialog5 extends BasePopWindow {

    private LeaseMapEntity entity;
    private TextView titleView;
    private TextView tvIsSelf;
    private TextView tvAddress;
    private TextView tvDistance;
    private TextView tvType;
    private TextView tvPrice;
    private TextView tvPriceUnit;
    private TextView tvExchange;
    private TextView tvInquiry;
    private TextView tvInvite;
    private TextView tvPhone;
    private TextView tvBrand;
    private TextView tvBrandLabel;
    private RelativeLayout rlClose;
    private CallBackListener callBackListener;


    public MapInfoDialog5(Context context) {
        super(context, DIR_DOWN_UP);
    }

    public MapInfoDialog5(Context context, AttributeSet attrs) {
        super(context, DIR_DOWN_UP);
    }

    @Override
    protected int popLayout() {
        return R.layout.map_info_dialog5;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        tvIsSelf = holder.getPop().findViewById(R.id.tv_isSelf);
        titleView = holder.getPop().findViewById(R.id.title_view);
//        tvAuthYear = holder.getPop().findViewById(R.id.tv_authYear);
        tvAddress = holder.getPop().findViewById(R.id.tv_address);
        tvDistance = holder.getPop().findViewById(R.id.tv_distance);
        tvType = holder.getPop().findViewById(R.id.tv_type);
        tvPrice = holder.getPop().findViewById(R.id.tv_price);
        tvPriceUnit = holder.getPop().findViewById(R.id.tv_price_unit);
        tvExchange = holder.getPop().findViewById(R.id.tv_exchange);
        tvInquiry = holder.getPop().findViewById(R.id.tv_inquiry);
        tvInvite = holder.getPop().findViewById(R.id.tv_invite);
        tvPhone = holder.getPop().findViewById(R.id.tv_phone);
        tvBrand = holder.getPop().findViewById(R.id.tv_brand);
        tvBrandLabel = holder.getPop().findViewById(R.id.tv_brand_label);
        rlClose = holder.getPop().findViewById(R.id.rl_close);
        rlClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dissMiss();
            }
        });
        RelativeLayout rootLayout = holder.getPop().findViewById(R.id.root_layout);
        rootLayout.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View view) {
                if (callBackListener == null || entity == null) {
                    return;
                }
                callBackListener.onClickItem(entity);
            }
        });

        tvExchange.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View view) {
                if (callBackListener == null || entity == null) {
                    return;
                }
                callBackListener.onCallExchange(entity);
            }
        });

        tvInquiry.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View view) {
                MToast.showToast(mContext, "功能开发中");
            }
        });

        tvInvite.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View view) {
                if (callBackListener == null || entity == null) {
                    return;
                }
                callBackListener.onCallInvite(entity);
            }
        });

        tvPhone.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View view) {
                if ("".equals(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LOGIN_TOKEN, ""))) {
                    showDialogLogin("请先完成登录，才能拨打电话");
                } else {
                    showTelDialog(entity.getPublisherPhone());
                }
            }
        });
    }

    public LeaseMapEntity getData() {
        return entity;
    }

    public MapInfoDialog5 setData(LeaseMapEntity entity) {
        this.entity = entity;
        titleView.setText(entity.getTitle());
        tvAddress.setText(entity.getAddress());
        tvDistance.setText(entity.getDistance());
        tvType.setText(entity.getCategory());
        tvBrand.setText("暂无信息");
        //机械。设备，机械 显示品牌型号
        if ("1".equals(entity.getType()) || "2".equals(entity.getType()) || "7".equals(entity.getType())) {
            String brandStr = "";
            if (!TextUtils.isEmpty(entity.getBrand())) {
                brandStr = entity.getBrand();
            }
            if (!TextUtils.isEmpty(entity.getModel())) {
                brandStr += entity.getModel();
            }
            tvBrand.setText(brandStr);
            if (TextUtils.isEmpty(brandStr)) {
                tvBrand.setText("暂无信息");
            }
            tvBrandLabel.setText("品牌型号：");
        } else if ("3".equals(entity.getType())) {
            //车牌显示车系
            tvBrandLabel.setText("车系：");
            if (!TextUtils.isEmpty(entity.getCarseries())) {
                tvBrand.setText(entity.getCarseries());
            }
        } else if ("4".equals(entity.getType()) || "5".equals(entity.getType()) || "6".equals(entity.getType())) {
            //房产，土地，工厂显示面积
            tvBrandLabel.setText("面积：");
            if (!TextUtils.isEmpty(entity.getRentArea())) {
                tvBrand.setText(entity.getRentArea() + "m²");
            }
        }

        tvPrice.setText(entity.getPrice());
        tvPriceUnit.setText(entity.getPriceUnit());
        if ("1".equals(entity.getIsSelf())) {
            tvIsSelf.setVisibility(View.VISIBLE);
        } else {
            tvIsSelf.setVisibility(View.GONE);
        }
        if (TextUtils.isEmpty(entity.getNegotiable())) {
            tvPrice.setText(entity.getPrice());
            tvPriceUnit.setVisibility(View.VISIBLE);
        } else {
            tvPrice.setText("面议");
            tvPriceUnit.setVisibility(View.GONE);
        }
        return this;
    }

    //拨打电话
    private void showTelDialog(String phone) {
        new RoundDialog.Builder(mContext)
                .title("确认呼叫")
                .content(phone)
                .confirm("拨打")
                .cancel("取消")
                .cancelable(false)
                .confirmCallback(v -> {
                    Intent intent = new Intent(Intent.ACTION_DIAL);
                    Uri data = Uri.parse("tel:" + phone);
                    intent.setData(data);
                    mContext.startActivity(intent);
                })
                .show();
    }

    private void showDialogLogin(String text) {
        new CommonDialog(mContext).builder().setPositiveButton("去登录").setNegativeButton("取消")
                .setTitle("温馨提示")
                .setMsg(text)
                .setCancelable(false)
                .setDialogCallBack(new CommonDialog.DialogCallBack() {
                    @Override
                    public void onRightClick() {
                        VerificationCodeLoginActivity.start(mContext);
                    }

                    @Override
                    public void onLeftClick() {

                    }
                }).show();
    }

    public interface CallBackListener {
        void onClickItem(LeaseMapEntity entity);

        void onCallExchange(LeaseMapEntity entity);

//         void onCallInquiry(LeaseMapEntity entity);

        void onCallInvite(LeaseMapEntity entity);
    }

    public MapInfoDialog5 setCallBackListener(CallBackListener dialogClickCallBack) {
        this.callBackListener = dialogClickCallBack;
        return this;
    }

}
